package com.pzhu.record.controller;

import com.pzhu.record.constants.ExcelReader;
import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Graduation;
import com.pzhu.record.entity.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Zhou YuSen
 * @date 2020/4/14 15:04
 */
@Controller
@RequestMapping("/graduation")
public class GraduationController extends BaseAction {

    @RequestMapping("/insertExcel")
    public String insertExcel(MultipartFile deletion, Model model) {
        ExcelReader<Graduation> graduationExcelReader = new ExcelReader<>();
        List<Graduation> graduationList = graduationExcelReader.readExcel(deletion, new Graduation());
        System.out.println(graduationList);
        for (Graduation list : graduationList) {
            Basicinformation basicinformation = basicinformationService.selectById(list.getBasicinformationId());
            if (basicinformation == null) {
                model.addAttribute("messageGraduation", "添加失败,不存在该学生");
                return "show";
            }
            System.out.println(list.getBasicinformationId());
        }
        int insertBatch = graduationService.insertBatch(graduationList);

        if (insertBatch == 0) {
            model.addAttribute("messageGraduation", "添加失败");
            return "show";
        }
        model.addAttribute("messageGraduation", "添加成功");
        return "show";
    }

    @RequestMapping("/insertExcelLack")
    public String insertExcelLack(MultipartFile deletion, Model model) {
        ExcelReader<Graduation> graduationExcelReader = new ExcelReader<>();
        List<Graduation> graduationList = graduationExcelReader.readExcel(deletion, new Graduation());
        System.out.println(graduationList);
        for (Graduation list : graduationList) {
            Basicinformation basicinformation = basicinformationService.selectById(list.getBasicinformationId());
            if (basicinformation == null) {
                model.addAttribute("messageGraduationLack", "添加失败");
                return "show";
            }
            System.out.println(list.getBasicinformationId());
        }
        int insertBatch = graduationService.updateBatch(graduationList);

        if (insertBatch == 0) {
            model.addAttribute("messageGraduationLack", "添加失败");
            return "show";
        }
        model.addAttribute("messageGraduationLack", "添加成功");
        return "show";
    }

    /**
     * 分页条件查询
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectLimit")
    public String selectPageUseDyc(HttpServletRequest request, Model model) {
        int pageIndex = 0;
        String keywords = request.getParameter("keywords");
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }
        Basicinformation basicinformation = new Basicinformation();
        if (keywords != null) {
            keywords = "%" + keywords + "%";
            basicinformation.setBasicinformationName(keywords);
        }
        Graduation graduation = new Graduation();
        graduation.setBasicinformation(basicinformation);

        Page<Graduation> graduationPage = graduationService.selectPage(pageIndex, graduation);

        model.addAttribute("graduationPage", graduationPage);

        return "show";
    }

    /**
     * 分页条件查询 学生
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectLimitStu")
    public String selectLimitStu(HttpServletRequest request, Model model, HttpSession session) {
        int pageIndex = 0;
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }

        Basicinformation basicinformation = (Basicinformation) session.getAttribute("userInfo");

        Graduation graduation = new Graduation();
        graduation.setBasicinformation(basicinformation);

        Page<Graduation> graduationPage = graduationService.selectPage(pageIndex, graduation);

        model.addAttribute("graduationPage", graduationPage);

        return "show";
    }

}
